package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.JwtTokenUtils;
import com.example.dao.AuditDao;
import com.example.dao.PaperDao;
import com.example.entity.Admin;
import com.example.entity.Audit;
import com.example.entity.Paper;
import com.example.entity.Params;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AuditService {
    @Resource
    private AuditDao auditDao;

    public PageInfo<Audit> findBySearch(Params params) {
        Admin user = JwtTokenUtils.getCurrentUser();
        if(!"管理员".equals(user.getRole())){
            params.setUserId(user.getId());
        }
        //分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());

        List<Audit> list = auditDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Audit audit) {
        Admin user = JwtTokenUtils.getCurrentUser();
        if (ObjectUtil.isNotNull(user)) {
            audit.setUserId(user.getId());
        }
        audit.setStatus("未审核");
        audit.setTime(DateUtil.now());
        auditDao.insertSelective(audit);
    }
    public void update(Audit audit) {
        audit.setStatus("未审核");
        Admin user = JwtTokenUtils.getCurrentUser();
        if("管理员".equals(user.getRole())){
            audit.setStatus("已审核");
        }
        auditDao.updateByPrimaryKeySelective(audit);
    }

    public void delete(Integer id) {
        auditDao.deleteByPrimaryKey(id);
    }

    public List<Audit> findAll() {
        return auditDao.selectAll();
    }
}
